#include "findwidget.h"
#include "ui_findwidget.h"
#include "widget.h"
#include "Core/tcpclient.h"
#include "Core/messagedispatcher.h"
FindWidget::FindWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FindWidget)
{
    ui->setupUi(this);
    LinkTcpSignalAndSlot();
}

FindWidget::~FindWidget()
{
    delete ui;
}

void FindWidget::LinkTcpSignalAndSlot()
{
    connect(MessageDispatcher::GetInstance()->getCsearchServer(),&CSearchService::EmitOnlineFindResult,
            this,&FindWidget::ActForOnlineFind);
    connect(this,&FindWidget::Sig_FindOnline,
            TcpClient::get_instance(),&TcpClient::SendMsg);
}

QListWidget *FindWidget::GetFindListWidget()
{
    return ui->FindList;
}


void FindWidget::ActForOnlineFind(QList<QPair<QString, QString> > RecvData)
{
    if(GetFindListWidget()==nullptr)return;
    GetFindListWidget()->clear();
    for(int i=0;i<RecvData.size();i++){
        GetFindListWidget()->addItem(RecvData[i].first);
    }
}

void FindWidget::on_OnlineBtn_clicked()
{
    QJsonObject Body;
    Body["USER"]=TcpClient::get_instance()->getUserName();
    emit Sig_FindOnline(MSG_TYPE_SEARCH_ONLINE_REQUEST,Body);
}
